package proxy;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Paths;

public class MyClassLoader1 extends ClassLoader {

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        String fileName = "d://TestServiceImpl.class";
        try {
            byte[] bytes = Files.readAllBytes(Paths.get(fileName));
            Class<?> clazz = defineClass(name, bytes, 0, bytes.length);
            if (clazz == null) {
                throw new ClassNotFoundException(name);
            }
            return clazz;
        } catch (IOException e) {
            throw new ClassNotFoundException(name, e);
        }
    }

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        MyClassLoader1 classLoader = new MyClassLoader1();
        Class<?> clazz = classLoader.loadClass("proxy.HelloWorldImpl");

        Object obj = clazz.newInstance();
        Method sayHello = clazz.getMethod("sayHello");
        sayHello.invoke(obj);
    }
}
